package com.shengde.jinwawaforleader.model.myJob.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.model.myJob.everyRecipe.EveryRecipeActivity;
import com.shengde.jinwawaforleader.model.myJob.everyRecipe.ModifyTheMenuActivity;
import com.shengde.jinwawaforleader.model.myJob.everyRecipe.PhotoViewZoomActivity;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProEveryRecipe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chason on 2016/7/16.
 */
public class FoodExpandableListAdapter extends BaseExpandableListAdapter{
    private static final String TAG = "FoodExpandableListAdapt";
    public List<ProEveryRecipe.DataEntity> parentList;
    public EveryRecipeActivity activity;
    private ArrayList<String> imgData = new ArrayList<>();
    private Context context;
    public FoodExpandableListAdapter(Context context,List<ProEveryRecipe.DataEntity> parentList, EveryRecipeActivity activity){
        this.context =context;
        this.parentList = parentList;
        this.activity = activity;
    }
    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return parentList.get(groupPosition).getGifted().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return parentList.get(groupPosition).getGifted().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder holder=null;
        if(convertView==null) {
            holder = new ParentViewHolder();
            convertView = View.inflate(activity, R.layout.expandable_view_header,null);
            holder.iv_meal = (ImageView) convertView.findViewById(R.id.iv_meal);
            holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
            convertView.setTag(holder);
        }else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        holder.textView1.setText(parentList.get(groupPosition).getDate());
        holder.textView2.setText(parentList.get(groupPosition).getWeek());
        if(isExpanded) {
            holder.iv_meal.setImageResource(R.drawable.icon_down);
        }else {
            holder.iv_meal.setImageResource(R.drawable.icon_more);
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder = null;
        if(convertView == null) {
            holder = new ChildViewHolder();
            convertView = View.inflate(activity,R.layout.expandable_view_content,null);
            holder.imageView_listitem = (ImageView) convertView.findViewById(R.id.imageView_listitem);
            holder.textView_grid_item00 = (TextView) convertView.findViewById(R.id.textView_grid_item00);
            holder.textView_grid_item = (TextView) convertView.findViewById(R.id.textView_grid_item);
            convertView.setTag(holder);
        }else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        Log.i(TAG, "getChildView: url = " + BaseProtocol.IMG_BASE+ parentList.get(groupPosition).getGifted().get(childPosition).getImg());
        Glide.with(activity).load(BaseProtocol.IMG_BASE+ parentList.get(groupPosition).getGifted().get(childPosition).getImg()).error(R.drawable.food).into(holder.imageView_listitem);
        holder.textView_grid_item00.setText(parentList.get(groupPosition).getGifted().get(childPosition).getTitle());
        holder.textView_grid_item.setText(parentList.get(groupPosition).getGifted().get(childPosition).getContent());
        imgData.clear();
        for(int i = 0; i < parentList.get(groupPosition).getGifted().size(); i++) {
            imgData.add(parentList.get(groupPosition).getGifted().get(i).getImg());
        }
        //图片点击监听
        holder.imageView_listitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, PhotoViewZoomActivity.class);
                intent.putStringArrayListExtra("imgData", imgData);
                intent.putExtra("position",childPosition);
                activity.startActivity(intent);
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                System.out.println(parentList.get(groupPosition).getGifted().get(childPosition).getContent());
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);

                builder.setIcon(android.R.drawable.ic_dialog_info);
                builder.setTitle("请选择您的操作");

                builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Intent intent = new Intent(activity, ModifyTheMenuActivity.class);

                        intent.putExtra("img", parentList.get(groupPosition).getGifted().get(childPosition).getImg());//: "http",
                        intent.putExtra("content", parentList.get(groupPosition).getGifted().get(childPosition).getContent());//: "蛋炒饭"
                        intent.putExtra("date", parentList.get(groupPosition).getGifted().get(childPosition).getDate());//: "2016-05-23",
                        intent.putExtra("title", parentList.get(groupPosition).getGifted().get(childPosition).getTitle());//: "午餐",
                        intent.putExtra("id", parentList.get(groupPosition).getGifted().get(childPosition).getId());//: "id",
                        activity.startActivityForResult(intent, Constant.REQUEST_CODE_ADD);

                    }
                });
                builder.setNeutralButton("删除", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, int whichButton) {
                        BaseProtocol.getInstance().delRecipe(context,FoodExpandableListAdapter.this,groupPosition,childPosition,dialog);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }

        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ParentViewHolder{
        TextView textView2;
        TextView textView1;
        ImageView iv_meal;
    }
    class ChildViewHolder{
        ImageView imageView_listitem;
        TextView textView_grid_item00;
        TextView textView_grid_item;
    }
}